<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <el-form
        ref="form"
        :model="queryForm.queryBean"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.phone"
            placeholder="手机号码"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.userName"
            placeholder="姓名"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        prop="userName"
        label="管理员名称"
        width="300"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltipcode
        label="手机号码"
        prop="phone"
        width="300"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="角色权限" width="300">
        <template #default="{ row }">
          <el-popover
            placement="right"
            title="角色:"
            width="200"
            trigger="click"
            :content="roleList"
          >
            <el-button slot="reference" @click="showRole(row)">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="用户类型"
        prop="roleId"
      ></el-table-column> -->
      <el-table-column show-overflow-tooltip label="操作" width="300">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleSet(row)">设置角色</el-button>
          <el-button
            v-if="row.userId != 1"
            type="text"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
          <el-button
            v-if="row.userId != 1"
            type="text"
            @click="handleSetProject(row)"
          >
            绑定项目
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <set-role-edit ref="roleEdit" @fetch-data="fetchData"></set-role-edit>
    <set-project ref="set" @fetch-data="fetchData"></set-project>
  </div>
</template>

<script>
  import { getUserAdminRole } from '@/api/userManagement'
  import { getUserList, deleteUser } from '@/api/user'
  import Edit from './components/UserManagementEdit'
  import SetRoleEdit from './components/SetRoleEdit.vue'
  import SetProject from './components/SetProject.vue'
  export default {
    name: 'UserManagement',
    components: { Edit, SetRoleEdit, SetProject },
    data() {
      return {
        roleList: '',
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: { roleId: '5' },
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleSetProject(row) {
        if (row.roleId) {
          this.$refs['set'].showEdit(row)
        } else {
          this.$refs['set'].showEdit()
        }
      },
      async showRole(row) {
        let userId = row.userId
        const { data } = await getUserAdminRole(userId)
        if (data.status === 0) {
          let _list = data.body.map((item) => {
            return item.roleName
          })
          this.roleList = _list.toString()
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      handleEdit(row) {
        if (row.roleId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSet(row) {
        if (row.roleId) {
          this.$refs['roleEdit'].showEdit(row)
        } else {
          this.$refs['roleEdit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.roleId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteUser({ userId: row.userId })
            if (data.status == 0) {
              this.$baseMessage('删除成功！', 'success')
            } else {
              this.$baseMessage(data.message, 'error')
            }
            this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getUserList(this.queryForm)
        const { body } = data
        this.list = body.data
        this.total = body.totalSize
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
